<template>
  <BasicModal
    v-bind="$attrs"
    @ok="handleSubmint"
    :minHeight="20"
    okText="提交"
    @register="register"
    :title="modalTitle"
    :loading="loading"
    @visible-change="handleVisibleChange"
  >
    <div class="pt-3px pr-3px">
      <BasicForm @register="registerForm" />
    </div>
  </BasicModal>
</template>

<script lang="ts" setup>
  import { BasicModal, useModal } from '/@/components/Modal';
  import { computed, nextTick, ref } from 'vue';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form/index';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { adddevicetype, updatedevicetype } from '/@/api/equipmentType/index';
  import { getPopupContainer } from '/@/utils';

  const props = defineProps({
    info: {
      type: Object,
      default: () => {},
    },
  });
  let loading = ref(false);
  let modalTitle = computed(() => {
    return `${props.info.type === 'add' ? '新建' : '编辑'}设备类型`;
  });
  // 表单内容
  const schemas: FormSchema[] = [
    {
      field: 'Code',
      component: 'Input',
      label: '类型编码：',
      colProps: {
        span: 24,
      },
      componentProps: {
        disabled: false,
      },
      rules: [
        {
          required: true,
          message: '请输入类型编码',
          trigger: ['change', 'blur'],
        },
      ],
    },
    {
      field: 'Name',
      component: 'Input',
      label: '类型名称：',
      colProps: {
        span: 24,
      },
      componentProps: {
        disabled: false,
      },
      rules: [
        {
          required: true,
          message: '请输入类型名称',
          trigger: ['change', 'blur'],
        },
      ],
    },
    // {
    //   field: 'Category',
    //   component: 'Select',
    //   label: '设备类别：',
    //   colProps: {
    //     span: 24,
    //   },
    //   componentProps: {
    //     placeholder: '请选择设备类别',
    //     getPopupContainer: () => getPopupContainer(),
    //     options: [
    //       { label: '生产设备', value: '1' },
    //       { label: '检测设备', value: '2' },
    //       { label: '辅助设备', value: '3' },
    //       { label: '厂务设备', value: '4' },
    //       { label: '仪器仪表', value: '5' },
    //       { label: '办公设备', value: '6' },
    //       { label: 'IT设备', value: '7' },
    //       { label: '其它', value: '8' },
    //     ],
    //   },
    //   rules: [
    //     {
    //       required: true,
    //       message: '请选择设备类别',
    //       trigger: ['change', 'blur'],
    //     },
    //   ],
    // },
  ];

  const [registerForm, { validateFields, setFieldsValue, resetFields, clearValidate }] = useForm({
    labelWidth: 120,
    schemas: schemas,
    showActionButtonGroup: false,
    actionColOptions: {
      span: 24,
    },
  });
  const { createMessage } = useMessage();
  const { t } = useI18n();
  const emit = defineEmits(['submitSuccess']);
  const [register] = useModal();

  async function handleSubmint() {
    try {
      let res = await validateFields();
      res.Category = +res.Category;
      if (props.info.type === 'add') {
        await adddevicetype([res]);
      } else {
        await updatedevicetype([{ Id: props.info.data.Id, ...res }]);
      }

      createMessage.success(t('layout.setting.operatingTitle'));
      emit('submitSuccess', {});
    } catch (error: any) {
      console.log('not passing', error);
    }
  }
  // 设置可选项

  function handleVisibleChange(visible) {
    if (visible) {
      nextTick(async () => {
        let newVal = props.info.data;
        if (props.info.type !== 'add') {
          // newVal.Category = newVal.Category.toString();
          setFieldsValue(newVal);
        }
        clearValidate();
      });
    } else {
      resetFields();
    }
  }
</script>

<style lang="less" scoped>
  .slot-select {
    width: 100%;
    height: 32px;
    padding: 0 11px;
    display: flex;
    position: relative;
    background-color: #fff;
    border: 1px solid #d9d9d9;
    border-radius: 2px;
    cursor: pointer;
  }

  .slot-select:hover {
    border: 1px solid #1e80ff;
  }

  .slot-select-con {
    margin-right: 18px;
    line-height: 30px;
    flex: 1;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
    color: #000000d9;
    font-size: 14px;
  }

  .con-placeholder {
    color: #bfbfbf;
  }

  .icon-style {
    line-height: 30px;
    font-size: 11px;

    color: #bfbfbf;
  }

  .erro-style {
    border: 1px solid #ed6f6f !important;
  }

  .slot-select-diabled {
    background: #f5f5f5;
    cursor: not-allowed;

    .slot-select-con {
      color: rgba(0, 0, 0, 0.25);
    }
  }

  .slot-select-diabled:hover {
    border: 1px solid #d9d9d9;
  }
</style>
